package com.dhecp.project.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.DateUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysModuleNotice;
import com.dhecp.project.system.mapper.SysModuleNoticeMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysModuleNoticeService;

/**
 * 通知 服务实现层
 * @author DH-Xxj
 * @version 2020年4月24日
 */
@Service
public class SysModuleNoticeServiceImpl implements SysModuleNoticeService {

	@Autowired
	private SysModuleNoticeMapper moduleNoticeMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询未读通知数
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public Integer queryModuleNoticeWds(Map<String, Object> params) {
		params.put("cUser", ShiroUtils.getMandatorId());
		return moduleNoticeMapper.selectWds(params);
	}

	/**
	 * 查询未读通知
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleNotice> queryModuleNoticeWd(Map<String, Object> params) throws Exception {
		params.put("cUser", ShiroUtils.getMandatorId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleNoticeMapper.selectWd(params);
	}
	
	/**
	 * 查询已读通知
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleNotice> queryModuleNoticeYd(Map<String, Object> params) throws Exception {
		params.put("cUser", ShiroUtils.getMandatorId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleNoticeMapper.selectYd(params);
	}
	
	/**
	 * 查询当前用户为发送人数据
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleNotice> queryModuleNoticeFs(Map<String, Object> params) throws Exception {
		params.put("bUser", ShiroUtils.getUserId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleNoticeMapper.selectFs(params);
	}

	/**
	 * 通知标记为已读
	 * @param infoItems 对应实体类的json格式字符串（仅需 guid）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateTaskStateToRead(Map<String, Object> params) {
		List<SysModuleNotice> moduleNoticeList = JSON.parseArray(params.get("infoItems").toString(), SysModuleNotice.class);
		if (moduleNoticeList == null || moduleNoticeList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		for (SysModuleNotice moduleNotice : moduleNoticeList) {
			moduleNotice.setTaskState("已读");
			moduleNotice.setDealTime(new Date());
			moduleNoticeMapper.updateTaskState(moduleNotice);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
     * 新增通知
     * 
     * @param 
     */
    @Override
	public AjaxResult insertModuleNotice(Map<String, Object> params){
    	
    	if (params.get("moduleTable") == null || params.get("moduleTable") == "") {
    		return AjaxResult.error("请提供必要的参数[功能唯一标识]：ModuleTable");
		}else{
			SysModuleNotice moduleNotice = new SysModuleNotice();
			
			moduleNotice.setbUser(ShiroUtils.getMandatorId());
			if(params.get("cUser")!=null && params.get("cUser")!="")moduleNotice.setcUser(params.get("cUser").toString());
	        if(params.get("pGuid")!=null && params.get("pGuid")!="")moduleNotice.setpGuid(params.get("pGuid").toString());
	        if(params.get("moduleTable")!=null && params.get("moduleTable")!="")moduleNotice.setModuleTable(params.get("moduleTable").toString());
			if(params.get("taskContent")!=null && params.get("taskContent")!="")moduleNotice.setTaskContent(params.get("taskContent").toString());
			if(params.get("taskAbstract")!=null && params.get("taskAbstract")!="")moduleNotice.setTaskAbstract(params.get("taskAbstract").toString());
			if(params.get("taskState")!=null && params.get("taskState")!="")moduleNotice.setTaskState(params.get("taskState").toString());
			if(params.get("pageTitle")!=null && params.get("pageTitle")!="")moduleNotice.setPageTitle(params.get("pageTitle").toString());
			if(params.get("pageURL")!=null && params.get("pageURL")!="")moduleNotice.setPageURL(params.get("pageURL").toString());
			if(params.get("dataURL")!=null && params.get("dataURL")!="")moduleNotice.setDataURL(params.get("dataURL").toString());
			moduleNotice.setCreateTime(new Date());
			if(params.get("limitTime")!=null && params.get("limitTime")!="")moduleNotice.setLimitTime(DateUtils.parseDate(params.get("limitTime")));
			
			moduleNoticeMapper.insertNotice(moduleNotice);
			return AjaxResult.success("操作成功");
    	}
    }

	@Override
	public List<Map<String, Object>> queryModuleNoticeWdWithWeChatId() throws Exception {
		return moduleNoticeMapper.selectWdWithWeChatId();
	}

	@Override
	public AjaxResult updateNoticeWxSendFlag(List<String> idList){
		if (idList == null && idList.size() == 0) {
			return AjaxResult.error("请提供必要的参数[idList]");
		}else {

			Map<String, Object> params = new HashMap<>();
			params.put("ids", idList);
			moduleNoticeMapper.updateNoticeWxSendFlag(params);
			return AjaxResult.success("操作成功");
		}
	}
}
